package com.archerdu.apply.controller.admin;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.archerdu.apply.model.AdvertiseCatalog;
import com.archerdu.apply.model.User;
import com.archerdu.apply.service.AdvertiseCatalogManager;
import com.archerdu.base.controller.BaseController;
import com.archerdu.base.exception.BussinessException;
import com.archerdu.commons.constant.DataConstant;

@Controller
@Scope("prototype")
@RequestMapping(value = "/admin/advertiseCatalog")
public class AdvertiseCatalogController
        extends BaseController {
    
    private static final long  serialVersionUID = 4707162553670697575L;
    
    private AdvertiseCatalogManager advertiseCatalogManager;
    private AdvertiseCatalog        advertiseCatalog;
    
    /**
     * 
     * @return 目录页面
     */
    @RequestMapping(value = "/tree")
    public String tree() {
        return "admin/advertiseCatalog/tree";
    }
    
    @RequestMapping(value = "/add")
    public ModelAndView add(HttpServletRequest requ, AdvertiseCatalogController mc) {
        advertiseCatalog = mc.getAdvertiseCatalog();
        ModelAndView mv = new ModelAndView();
        mv.setViewName("admin/advertiseCatalog/add");
        if ("get".equalsIgnoreCase(requ.getMethod())) {
            mv.addObject(advertiseCatalog);
        }
        else {
            try {
                advertiseCatalog.setCreateUser(((User) requ.getSession().getAttribute(DataConstant.SESSION_SYS_USER))
                        .getId());
                advertiseCatalog.setCreatetime(getSqlDate());
                advertiseCatalogManager.add(advertiseCatalog);
                addMessage(DataConstant.COMMON_ADD_SUCCESS);
            }
            catch (BussinessException e) {
                addMessage(e.getMessage());
                logger.error(e.getMessage(), e);
            }
            catch (Exception e) {
                addMessage(DataConstant.COMMON_ADD_FAILED);
                logger.error(e.getMessage(), e);
            }
        }
        mv.addObject("message",getMessage());
        return mv;
    }
    
    /**
     * 文章目录编辑
     * 
     * @param requ
     * @param advertiseCatalog
     * @return ModelAndView
     */
    @RequestMapping(value = "/edit")
    public ModelAndView edit(HttpServletRequest requ, AdvertiseCatalogController mc) {
        
        logger.debug("advertiseCatalog.id is " + mc.getAdvertiseCatalog().getId());
        advertiseCatalog = mc.getAdvertiseCatalog();
        
        if ("get".equalsIgnoreCase(requ.getMethod())) {
            if (!StringUtils.isEmpty(advertiseCatalog.getId())) {
                advertiseCatalog = advertiseCatalogManager.get(advertiseCatalog.getId());
            }
        }
        else {
            advertiseCatalog.setUpdateUser(((User) requ.getSession().getAttribute((DataConstant.SESSION_SYS_USER))).getId());
            advertiseCatalog.setUpdatetime(getSqlDate());
            try {
                advertiseCatalogManager.modify(advertiseCatalog);
                advertiseCatalog = advertiseCatalogManager.get(advertiseCatalog.getId());
                addMessage(DataConstant.COMMON_EDIT_SUCCESS);
            }
            catch (Exception e) {
                addMessage(DataConstant.COMMON_EDIT_FAILED);
                logger.error(e.getMessage(), e);
            }
        }
        
        ModelAndView mav = new ModelAndView();
        mav.addObject(getMessage());
        mav.addObject(advertiseCatalog).setViewName("admin/advertiseCatalog/edit");
        return mav;
    }
    
    /**
     * 目录json字符串
     * 
     * @param response
     * @param writer
     * @param acc
     */
    @RequestMapping(value = "/json")
    public void json(HttpServletResponse response, Writer writer, AdvertiseCatalogController acc) {
        response.setContentType("text/plain");
        response.setCharacterEncoding("utf-8");
        advertiseCatalog = acc.getAdvertiseCatalog();
        if (advertiseCatalog != null && advertiseCatalog.getId() != null) {
            advertiseCatalog = advertiseCatalogManager.get(advertiseCatalog.getId());
        }
        else {
            advertiseCatalog = new AdvertiseCatalog();
            advertiseCatalog.setId("0");
        }
        try {
            writer.write(advertiseCatalogManager.loadJson(advertiseCatalog));
        }
        catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
    }
    
    public AdvertiseCatalogManager getAdvertiseCatalogManager() {
        return advertiseCatalogManager;
    }
    
    public void setAdvertiseCatalogManager(AdvertiseCatalogManager advertiseCatalogManager) {
        this.advertiseCatalogManager = advertiseCatalogManager;
    }
    
    public AdvertiseCatalog getAdvertiseCatalog() {
        return advertiseCatalog;
    }
    
    public void setAdvertiseCatalog(AdvertiseCatalog advertiseCatalog) {
        this.advertiseCatalog = advertiseCatalog;
    }
    
}
